package com.yang.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Enums;
import com.yang.mall.dto.ProductSortDTO;
import com.yang.mall.entity.ProductSort;
import com.yang.mall.mapper.ProductSortMapper;
import com.yang.mall.service.IProductSortService;
import com.yang.mall.tools.BeanUtils;
import com.yang.mall.vo.ProductSortVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author kadorlin
 * @since 2022-10-21
 */
@Service
public class ProductSortServiceImpl extends ServiceImpl<ProductSortMapper, ProductSort> implements IProductSortService {

    /**
     * 拉取分类
     * @param sortId
     * @return productSortVo
     */
    @Override
    public List<ProductSortVO> pullSort(Long sortId) {
        QueryWrapper<ProductSort> qw= new QueryWrapper<>();
        qw.lambda()
                .eq(Objects.nonNull(sortId),ProductSort::getSortId, sortId)
                .eq(ProductSort::getDelFlag, 0)
                .orderByDesc(ProductSort::getCreateTime);
        List<ProductSort> productSorts = this.list(qw);
        if (productSorts.isEmpty()) {
            return new ArrayList<>();
        }
        return BeanUtils.copyBeanProp(productSorts, ProductSortVO.class);
    }

    /**
     * 删除分类
     *
     * @param sortIds
     * @Author Cyj
     * @Description
     * @Return void
     * @Date 2021/8/30
     */
    @Override
    public void deleteSort(List<Long> sortIds) {
        UpdateWrapper<ProductSort> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .in(ProductSort::getSortId,sortIds)
                .set(ProductSort::getDelFlag,1)
                .set(ProductSort::getUpdateTime,new Date());
        this.update(updateWrapper);
    }

    /**
     * 创建分类
     *
     * @param productSortDTO
     * @Method createSort
     * @Author Cyj
     * @Description
     * @Return void
     * @Date 2021/8/30
     */
    @Override
    public void createSort(ProductSortDTO productSortDTO) {
        ProductSort productSort = BeanUtils.copyBeanProp(productSortDTO,ProductSort.class);
        productSort.setCreateTime(LocalDateTime.now());
        productSort.setDelFlag("0");
        this.saveOrUpdate(productSort);
    }

    /**
     * 查询分类列表
     * @param productSortDTO
     * @author:Cyj
     * @date:2021/8/30 15:55
     * @return:com.baomidou.mybatisplus.core.metadata.IPage<run.olive.common.core.vo.liveProduct.ProductSortVO>
     **/
    @Override
    public IPage<ProductSort> querySort(ProductSortDTO productSortDTO) {
        QueryWrapper<ProductSort> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(Objects.nonNull(productSortDTO.getSortId()),ProductSort::getSortId,productSortDTO.getSortId())
                .eq(ProductSort::getDelFlag,0)
                .like(Objects.nonNull(productSortDTO.getSortName()),ProductSort::getSortName,productSortDTO.getSortName())
                .orderByDesc(ProductSort::getCreateTime);

        Page<ProductSort> page = new Page<>(productSortDTO.getPageNum(), productSortDTO.getPageSize());
        return baseMapper.selectPage(page,qw);
    }


}
